# Flink + Python 3.11 镜像（用于 JobManager 和 TaskManager）
FROM flink:1.20-java11

LABEL maintainer="Browse ETL Team"
LABEL description="Flink 1.20 with Python 3.11 for PyFlink cluster mode"

# 安装 Python 3.11
RUN apt-get update && apt-get install -y \
    python3.11 \
    python3-pip \
    python3.11-dev \
    && rm -rf /var/lib/apt/lists/*

# 设置 Python 默认版本
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1 && \
    update-alternatives --install /usr/bin/python python /usr/bin/python3.11 1

# 第一步：安装 apache-flink（这层已经缓存，不会重复下载）
RUN pip3 install --no-cache-dir apache-flink==1.20.3 || \
    (pip3 install --upgrade pip setuptools wheel && \
     pip3 install --no-cache-dir apache-flink==1.20.3)

# 第二步：安装应用运行时依赖（只下载这些新包，约 50-100MB）
RUN pip3 install --no-cache-dir \
    flink-agents==0.1.0 \
    boto3>=1.34.0 \
    psycopg2-binary>=2.9.0 \
    requests>=2.32.0 \
    httpx>=0.28.0

# 下载 Kinesis Connector JAR（Table API 流式消费 Kinesis）
# 放到 /opt/flink/lib 目录，Flink 会自动加载
RUN wget -q https://repo1.maven.org/maven2/org/apache/flink/flink-sql-connector-kinesis/5.0.0-1.20/flink-sql-connector-kinesis-5.0.0-1.20.jar \
    -O /opt/flink/lib/flink-sql-connector-kinesis-5.0.0-1.20.jar && \
    echo "✅ Kinesis Connector JAR 下载完成"

# 验证安装
RUN python3 --version && pip3 --version && \
    python3 -c "from pyflink.datastream import StreamExecutionEnvironment; print('PyFlink OK')" && \
    python3 -c "import flink_agents; print('Flink Agents OK')" && \
    ls -lh /opt/flink/lib/flink-sql-connector-kinesis-5.0.0-1.20.jar

